package com.jiangds.open.util;


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * {@link TimeUtil}  根据时间解析器 解析时间为LocalDateTime格式
 *
 * @author jiangliang
 * @since 2021/1/7 15:28
 */
public class TimeUtil {


    public static LocalDateTime parseString(String dateString,String formatterString){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatterString);
        try {
          return LocalDateTime.parse(dateString, formatter);
        }catch (Exception e1){
            //如果日期解析失败了,用LocalDate方案再次解析一下
            int dateLength = dateString.length();
            if (formatterString.length() > dateLength) {
                formatter = DateTimeFormatter.ofPattern(formatterString.substring(0, dateLength));
            }
            LocalDate localDate = LocalDate.parse(dateString, formatter);
            return LocalDateTime.of(localDate, LocalTime.now());
        }
    }
}
